import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Test2 {
    private static int[] x = {-1,0,0,1};
    private static int[] y = {0,-1,1,0};
    private static boolean[][] vis;
    private static int minStep = 1000001;
    private static int path;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int row = in.nextInt();
            int col = in.nextInt();
            in.nextLine();
            int xs = in.nextInt()-1;
            int ys = in.nextInt()-1;
            int xt = in.nextInt()-1;
            int yt = in.nextInt()-1;
            in.nextLine();
            char[][] board = new char[row][col];
            vis = new boolean[row][col];
            for (int i = 0; i < row; i++) {
                String str = in.nextLine();
                board[i] = str.toCharArray();
            }
            if (board[xt][yt] == '*') {
                System.out.println(-1);
                return;
            }
            vis[xs][ys] = true;
            int ret = bfs(board, xs, ys, xt, yt);
            System.out.println(ret);
        }
    }

    private static int bfs(char[][] board, int xs, int ys, int xt, int yt) {
        int row = board.length;
        int col = board[0].length;
        boolean[][] vis = new boolean[row][col];
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[]{xs, ys, 0});
        vis[xs][ys] = true;

        int[] dx = {-1, 0, 0, 1};
        int[] dy = {0, -1, 1, 0};

        while (!queue.isEmpty()) {
            int[] cur = queue.poll();
            int x = cur[0], y = cur[1], step = cur[2];
            if (x == xt && y == yt) return step;
            for (int i = 0; i < 4; i++) {
                int nx = x + dx[i];
                int ny = y + dy[i];
                if (nx >= 0 && nx < row && ny >= 0 && ny < col
                        && board[nx][ny] == '.' && !vis[nx][ny]) {
                    vis[nx][ny] = true;
                    queue.offer(new int[]{nx, ny, step + 1});
                }
            }
        }
        return -1;
    }
}
